package com.forum.LoginServlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.forum.domain.User;
import com.forum.domain.UserDAO;

public class Register extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Register() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html ; charset = utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();

		String type = request.getParameter("type");
		if (type != null) {
			if (type.equals("check")) {
				String email = request.getParameter("email");
				if (email != null) {
					UserDAO userDao = new UserDAO();
					List<User> user = userDao.findByEmail(email);
					if (user.size() == 0)
						out.println("可以注册");
					else
						out.println("已经被注册了");
				}
			}
		} else {
			String email = request.getParameter("email");
			String password = request.getParameter("password");
			String nickname = request.getParameter("nickname");
			String realname = request.getParameter("realname");
			String introduction = request.getParameter("introduction");
			
			UserDAO userDao = new UserDAO();
			List<User> user = userDao.findByEmail(email);
			if (user.size() != 0)
				response.sendRedirect("page/register.jsp");
			
			String url = request.getParameter("url");
			if (validate(email, password, nickname, realname, introduction) == true) {
				User newUser = new User(0, email, password, realname, nickname,
						0, 0, 0, 0);
				if(!url.equals(""))
					newUser.setPicurl(url);
				else {
					newUser.setPicurl("default.jpg");
				}
				newUser.setIntroduction(introduction);
				userDao.save(newUser);
				userDao.getSession().beginTransaction().commit();
				request.getSession().setAttribute("login", "true");
				request.getSession().setAttribute("id", newUser.getUserid());
				response.sendRedirect("page/home.jsp");
				out.println("zhucechenggong");
			} else {
				out.println("注册失败");
			}
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

	boolean validate(String email, String password, String nickname,
			String realname, String introduction) {
		UserDAO userDao = new UserDAO();
		List<User> user = userDao.findByEmail(email);
		if (user.size() != 0)
			return false;

		String emailCheck = "^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*[0-9a-zA-Z]+))@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2}|net|com|gov|mil|org|edu|int|cn)$";
		Pattern regex = Pattern.compile(emailCheck);
		Matcher matcher = regex.matcher(email);
		boolean isEmailMatched = matcher.matches();

		boolean bPW = false;
		if (password.length() >= 6 && password.length() <= 12)
			bPW = true;

		boolean bNick = false;
		if (nickname.length() >= 1 && nickname.length() <= 18)
			bNick = true;
		introduction = introduction.trim();

		return isEmailMatched && bPW && bNick;
	}
}
